package edu.jamestang.yeb.controller;

import edu.jamestang.yeb.entity.Position;
import edu.jamestang.yeb.service.PositionService;
import edu.jamestang.yeb.util.Resp;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @author JamesTang
 * @date 2021/12/14 23:16
 */
@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {


    @Autowired
    private PositionService positionService;

    @ApiOperation(value = "get all position info")
    @GetMapping("/")
    public List<Position> getAllPositions(){
        return positionService.list();
    }

    @ApiOperation(value = "add position info")
    @PostMapping("/")
    public Resp addPosition(@RequestBody Position position){
        position.setCreateDate(LocalDateTime.now());
        if(positionService.save(position)){
            return Resp.success("Add complete");
        }
        return Resp.error("Add error");
    }


    @ApiOperation(value = "update position info")
    @PutMapping("/")
    public Resp updatePosition(@RequestBody Position position){

        if(positionService.updateById(position)){
            return Resp.success("Update success");
        }
        return Resp.error("Update error");
    }

    @ApiOperation(value = "delete position")
    @DeleteMapping("/{id}")
    public Resp deletePosition(@PathVariable Integer id){
        if(positionService.removeById(id)){
            return Resp.success("Delete success");
        }
        return Resp.error("Delete error");
    }

    @ApiOperation(value = "batch delete")
    @DeleteMapping("/")
    public Resp deletePositionsByIds(Integer[] ids){
        if(positionService.removeByIds(Arrays.asList(ids))){
            return Resp.success("Delete complete");
        }

        return Resp.error("Delete error");
    }
}
